home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.1 / Libraries / DOS / readargs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  2.5 KB  |  94 lines

  1. /* This is an example of the command-line processing for a print program */
  2.  
  3. #include <exec/types.h>
  4. #include <dos/dos.h>
  5. #include <dos/rdargs.h>
  6. #include <clib/dos_protos.h>
  7.  
  8. /* normally you'ld include pragmas here */
  9.  
  10. #define TEMPLATE          "Files/A/M,Header/K,Spool/S,Page=PageNumber/K/N"
  11. #define OPT_FILES         0
  12. #define OPT_HEADER        1
  13. #define OPT_SPOOL         2
  14. #define OPT_PAGE          3
  15. #define OPT_COUNT         4
  16.  
  17. LONG opts[OPT_COUNT];        /* C guarantees this will be all 0's! */
  18.  
  19. int
  20. main (argc,argv)
  21.     int argc;
  22.     char **argv;
  23. {
  24.     struct RDArgs *argsptr;
  25.     char **sptr;
  26.  
  27.  
  28.       /*==================================================================*/
  29.       /* If ReadArgs() sees anything but zeros passed to it in elements   */
  30.       /* of this array, ReadArgs() will assume that they are defaults.    */
  31.       /*==================================================================*/
  32.       argsptr = ReadArgs(TEMPLATE, opts, NULL);
  33.  
  34.       /*=================================================================*/
  35.       /* argsptr will be NULL if ReadArgs() failed, the secondary result */
  36.       /* code is fetched by IoErr().                                     */
  37.       /*=================================================================*/
  38.       if (argsptr == NULL)
  39.       {
  40.         PrintFault(IoErr(), NULL);    /* prints the appropriate err message */
  41.     return RETURN_ERROR;
  42.       }
  43.       else
  44.       {
  45.     sptr = (char **) (opts[OPT_FILES]);
  46.         if (!sptr)
  47.         /* this can never actually happen, due to /A */
  48.         VPrintf("No files specified!\n",NULL);
  49.     else {
  50.         VPrintf("files specified:\n",NULL);
  51.  
  52.         /* last string ptr is NULL */
  53.         while (*sptr)
  54.         {
  55.             /* VPrintf takes a ptr to an array of arguments! */
  56.             VPrintf("\t%s\n",(LONG *) sptr);
  57.             sptr++;
  58.         }
  59.     }
  60.     /* if option was not specified, it will be NULL (since buffer started */
  61.     /* with opt[] array all NULL).                          */
  62.  
  63.     if (opts[OPT_HEADER])
  64.     {
  65.         /* remember, opts[OPT_HEADER] has a string ptr, and */
  66.         /* VPrintf wants a pointer to that pointer.         */
  67.         VPrintf("Header is '%s'\n",&(opts[OPT_HEADER]));
  68.     }
  69.  
  70.     if (opts[OPT_SPOOL])
  71.     {
  72.         VPrintf("Spooling selected\n",NULL);
  73.     }
  74.  
  75.     /* remember, it's a pointer to a long! */
  76.     if (opts[OPT_PAGE])
  77.     {
  78.         /* the actual number can be accessed by
  79.             *((LONG *) opts[OPT_PAGE])
  80.         */
  81.  
  82.         /* VPrintf takes a ptr to an array of arguments! */
  83.         VPrintf("Asked to print page %ld\n",(LONG *) opts[OPT_PAGE]);
  84.     }
  85.  
  86.     /* normally the meat of the program would go here */
  87.  
  88.     /* cleanup */
  89.     FreeArgs(argsptr);
  90.       }
  91.  
  92.       return RETURN_OK;        /* program succeeded */
  93. }
  94.